lobsterlayui.main(['index', 'layer', 'form', 'element', 'table', 'laydate'], {
    data: {
        table: null,
        tableoptions: {},
        layerindex: 0,
        selectdata: {},
    },
    pageload: function (right) {
        console.log(right);
        this.initevent();
        this.loaddata();
    },
    check() {
        var self = this;
        var now = new Date();
        var date = $("#date").val() || "";
        var sdate = date.split(' ~ ')[0];
        var qiantian = new Date(now.getTime() - 24 * 60 * 60 * 1000);
        var month3before = self.formatDate(new Date(qiantian.setMonth(qiantian.getMonth() - 3)));
        if (sdate < month3before) {
            return true;
        }
        return false;
    },

    //初始化事件
    initevent: function () {
        var self = this;
        //查找reload,通过条件，查询网格的table重载数据显示到界面
        $("#btnSearch").click(function () {
            if (self.check()) {
                layer.msg('不能查询3个月以前的账单');
                return;
            }
            self.loaddata();
        });
        $("#excel").click(function () {
            if (self.check()) {
                layer.msg('不能下载3个月以前的账单');
                return;
            }
            self.report();
        })
        var date = new Date();
        var qiantian = self.formatDate(new Date(date.getTime() - 24 * 60 * 60 * 1000))
        layui.laydate.render({
            elem: '#date'
            , type: 'date'
            , range: '~'
            , btns: ['confirm']
            , value: self.formatDate(new Date(date.getTime() - 7 * 24 * 60 * 60 * 1000)) + ' ~ ' + qiantian
            , format: 'yyyy-MM-dd'
            , max: qiantian
        });
    },
    //加载订单列表
    loaddata: function () {
        var self = this;
        var date = $("#date").val() || "";
        self.data.table = layui.table.render({
            elem: '#datatable',
            where: {
                type: $("[name='ttype']").val(),
                sdate: date.split(' ~ ')[0],
                edate: date.split(' ~ ')[1],
            },
            headers: lobsterlayui.GetHeaderData(),
            url: lobsterlayui.GetLowCodeUrl("tspay.gettransactionlist"),
            height: 'full-130',
            cellMinWidth: 80,
            size: 'sm',
            page: true,
            cols: [[
                { width: "200", field: 'TransactionDate', title: '交易时间' },
                { width: "200", field: 'SubMchId', title: '商户号' },
                { width: "200", field: 'OrderNo', title: '支付订单号' },
                { width: "200", field: 'BusinessOrderNo', title: '商户订单号' },
                { width: "200", field: 'TradeNo', title: '交易流水号' },
                { width: "200", field: 'RefundNo', title: '退款单号' },
                { width: "200", field: 'BusinessRefundNo', title: '商户退款单号' },
                { width: "200", field: 'RefundTradeNo', title: '退款交易流水号' },
                { width: "80", field: 'PayFee', title: '订单金额' },
                { width: "80", field: 'RefundFee', title: '申请退款金额' }
            ]],
            parseData: function (res) {
                return {
                    code: res.code,
                    msg: res.msg,
                    count: res.data.count,
                    data: res.data.data
                }
            },
        })
    },
    formatDate: function (date) {
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? '0' + m : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        return y + '-' + m + '-' + d;
    },
    report() {
        var self = this;
        var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
        var date = $("#date").val() || "";
        var data = {
            type: $("[name='ttype']").val(),
            sdate: date.split(' ~ ')[0],
            edate: date.split(' ~ ')[1],
        }
        lobsterlayui.GETAPI('tspay.getbillreport', data).then(retdata => {
            layer.msg('获取成功');
            layer.close(load);
        })
    },
})